<template>
  <el-container>
    <el-aside width="55%"></el-aside>
    <el-main>
      <el-form
        :model="loginForm"
        status-icon
        :rules="rules"
        ref="loginForm"
        label-width="100px"
      >
        <el-form-item><h1 style="font-size: 24px">登 录</h1></el-form-item>
        <el-form-item label="账号" prop="username">
          <el-input
           prefix-icon="el-icon-user"
            type="text"
            v-model="loginForm.username"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input
            prefix-icon="el-icon-lock"
            type="password"
            v-model="loginForm.password"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="doLogin()">登录</el-button>
        </el-form-item>
        <el-form-item>
          <el-button @click="$router.push('/register')">注册</el-button>
        </el-form-item>
      </el-form>
    </el-main>
  </el-container>
</template>
<script>
import { mapActions } from 'vuex'
import { mixin } from '@/mixins'
import { login } from '@/api/otherApi'
export default {
  mixins: [mixin],
  data() {
    // 账户不能为空
    const validateUsername = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入账号'))
      } else {
        if (this.loginForm.checkUsername !== '') {
          this.$refs.loginForm.validateField('checkUsername')
        }
        callback()
      }
    }
    // 密码不能为空
    const validatePassword = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入密码'))
      } else {
        if (this.loginForm.checkPassword !== '') {
          this.$refs.loginForm.validateField('checkPassword')
        }
        callback()
      }
    }
    return {
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { validator: validateUsername, trigger: 'blur' }
        ],
        password: [
          { validator: validatePassword, trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    ...mapActions(['setUser']),
    // 登录
    doLogin() {
      this.$refs.loginForm.validate((valid) => {
        if (valid) {
          login(this.loginForm).then(res => {
            if (res.state === 200) {
              this.setUser(res.data)
              localStorage.setItem('USER_INFO', JSON.stringify(res.data))
              window.location.href = '/'
            } else if (res.state === 201) {
              this.showError(res.msg)
            }
          })
        } else {
          this.showError('error submit!!')
          return false
        }
      })
    }
  }
}
</script>

<style scoped>
.el-container {
  background: url("../assets/imgs/login-bgc.jpg") no-repeat;
  background-size: 100% 100%;
}
.el-main {
  margin: 0 auto;
  background: none;
}
.el-form {
  margin: 65px auto;
  width: 58%;
  height: 350px;
  text-align: center;
  background-color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
  padding: 20px 70px 20px 0px;
  border-radius: 12px;
}
.el-button {
  width: 100%;
}
</style>
